package com.wyh.common.config;

import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();//启用默认Servlet处理，处理静态资源请求
    }

    /**
     * 上传的文件解析器
     * @return
     */
    public StandardServletMultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }
    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")//拦截所有以/api开头的请求
                .allowedOrigins("*")//允许请求的域名
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")//允许请求的方法
                .allowedHeaders("*")//允许请求的头部信息
                .allowCredentials(false)
                .maxAge(3600);
    }
}
